{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "machine_shape": "hm",
      "gpuType": "T4",
      "collapsed_sections": [
        "klzSfQPfwSNV"
      ],
      "authorship_tag": "ABX9TyMR8mfYYgsBG/AN5wfdukX6",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/dream80/roop_colab/blob/main/ROOP%EF%BC%9A%E4%B8%80%E5%BC%A0%E5%9B%BE%E7%89%87%E4%B8%80%E9%94%AE%E6%8D%A2%E8%84%B8.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##一张图片，一个视频，一键换脸！\n",
        "\n",
        "\n",
        "基于[roop](https://github.com/s0md3v/roop)项目，方便使用的一个colab脚本。\n",
        "\n",
        "仅供学习研究，请勿用于任何非法用途！\n",
        "\n",
        "脚本非常简单，应该没啥要解释的，如果有问题，通过下方渠道联系我\n",
        "\n",
        "公众号：托尼不是塔克  \n",
        "博客：[tonyisstark.com](https://www.tonyisstark.com/)"
      ],
      "metadata": {
        "id": "glwNVbldZ1Id"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ZqdQ95WTKCSQ",
        "cellView": "form"
      },
      "outputs": [],
      "source": [
        "#@title 1.克隆源代码\n",
        "!git clone https://github.com/s0md3v/roop.git \n",
        "!git clone https://github.com/dream80/roop_colab.git\n"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#@title 2.进入项目\n",
        "%cd /content/roop\n",
        "!git checkout be0b57e47a72e8f8f76375a3e2d577abd9486391"
      ],
      "metadata": {
        "id": "FnSwYDX5K1P2",
        "cellView": "form"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#@title 3.安装依赖\n",
        "#有红色警告不用担心！\n",
        "\n",
        "!pip install -r requirements.txt"
      ],
      "metadata": {
        "id": "f6urrvLGKqcU",
        "cellView": "form"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#@title 4.下载模型\n",
        "!wget https://github.com/dream80/roop_colab/releases/download/v0.0.1/inswapper_128.onnx"
      ],
      "metadata": {
        "id": "mvgmdOpaQRlT",
        "cellView": "form"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#@title 5.开始换脸\n",
        "\n",
        "GPU = True #@param {type:\"boolean\"}\n",
        "\n",
        "if GPU : \n",
        "  !python run.py --gpu-vendor nvidia -f /content/roop_colab/1.jpg -t /content/roop_colab/2.mp4 -o /content/roop_colab/out.mp4\n",
        "else:\n",
        "  !python run.py -f /content/roop_colab/1.jpg -t /content/roop_colab/2.mp4 -o /content/roop_colab/out.mp4\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "cMJi9bI3LUyX",
        "cellView": "form"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "勾选GPU，则使用GPU，如果你GPU配额用完了，可以去掉勾选，用CPU顶一下。或者开Pro+"
      ],
      "metadata": {
        "id": "iOOzIywkxw9o"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "#补充说明\n",
        "\n",
        "  \n"
      ],
      "metadata": {
        "id": "P_cIVG84cTX3"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "## 视频文件在哪里？  \n",
        "\n",
        "换脸成功后的视频保存在roop_colab文件里面  \n"
      ],
      "metadata": {
        "id": "O8qkUHNNw8-z"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 如何使用自己的素材？  \n",
        "\n",
        "如果要替换图片和视频，只需要在左边的文件管理里面右键上传，然后改一下名字就可以。了。  \n",
        "素材文件都在roop_colab文件中   \n",
        "1.jpg：换脸图片  \n",
        "2.mp4：换脸视频  \n",
        "out.mp4：是换脸后的视频  \n",
        "\n",
        "点击左侧文件，找到MP4,右键可以下载！\n",
        "\n"
      ],
      "metadata": {
        "id": "pl3m7Ymmw3A8"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 解除NSFW封印\n",
        "\n",
        "如果第五步没有成功，大概率是被监测到涩涩了。  \n",
        "在左边目文件管理里面找到roop/core.py，双击打开找到 predict_video_frames,predict_image相关的行和quit()函数删掉就可以了。"
      ],
      "metadata": {
        "id": "kUXiEHjCxMpt"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "## 大文件处理\n",
        "\n",
        "如果文件比较大，直接在Colab上传会有难度，可以先传到谷歌云盘。然后挂载云盘就可以了，修改一下第五步里面图片和视频的地址就可以了。挂载谷歌盘之后也可以把输出文件直接指向云盘。 \n",
        "\n",
        "如何需要使用谷歌云盘的文件，可以点击这个脚本，挂载云盘。\n",
        "\n",
        "from google.colab import drive  \n",
        "drive.mount('/content/drive')"
      ],
      "metadata": {
        "id": "klzSfQPfwSNV"
      }
    }
  ]
}